//
//  UserAccount.swift
//  XinLangWeibo10
//
//  Created by 白利兵 on 15/10/17.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit

class UserAccount:NSObject,NSCoding {
    var access_token: String?
    var expires_in: NSTimeInterval = 0 {
        didSet{
            expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    var expiresDate: NSDate?
    var uid: String?
    /// 用户昵称
    var screen_name: String?
    /// 用户头像地址（大图），180×180像素
    var avatar_large: String?
    // 字典转模型
    init(dict: [String :AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    // 重写未定义的属性的方法
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    // 重写description方法
    override var description: String {
        let key = ["access_token","expires_in","expiresDate","uid","screen_name","avatar_large"]
        return dictionaryWithValuesForKeys(key).description
    }

    // 键值编码
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
    // 键值解码
    /// 解档 - 从磁盘加载二进制文件，转换成对象时调用 － 跟网络的反序列化很像
    ///
    /// - parameter aDecoder: 解码器
    ///
    /// - returns: 当前对象
    /// `required` - 没有继承性，所有的对象只能解档出当前的类对象
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        uid = aDecoder.decodeObjectForKey("uid") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
    }
}
















